/*
 * @Author: wangming
 * @Date: 2022-04-27 16:42:58
 * @LastEditors: wangming
 * @LastEditTime: 2022-05-18 17:16:26
 * @FilePath: /hanma-paas-mobile-fed/Users/wm/瀚川/hanma-application-designer-fed/src/components/BlocklyEditor/desktop.utils.js
 * @Description:
 */
import storeform from "@/store";
import { transformTreeFormRule } from "@/utils/common.js";
/**
 * 获取所有模态框
 * @returns [{cid,cname},...]
 */
function getModals() {
  let { template } = storeform.getters.getFormDataForMain || [];
  return template.map((i) => ({ cid: i.key, cname: i.name }));
}

/**
 * 获取页面所有组件，包含主页面以及模态框中的组件
 * @returns [{cid,cname,ctype,cscope},...]
 */
function getComponents() {
  return [];
}
/**
 * 获取所在模态框页面key
 * @returns [{cid,cname,ctype,cscope},...]
 */
function getScope() {
  let { getNavName, getFormData } = storeform.getters;
  if (getNavName === "template") {
    return getFormData.key;
  }
}

/**
 * 获取指定作用域（模态框或者主页面）中的组件
 * @param {*} scope
 * @returns [{cid,cname,ctype},...]
 */
function getScopedComponents(scope = undefined) {
  let { template } = storeform.getters.getFormDataForMain || [];
  let currentScope = scope || getScope();
  let modelTreeList = currentScope
    ? template.find((i) => i.key === currentScope)?.formList
    : storeform.getters.getMainPageList;
  return transformTreeFormRule(
    modelTreeList,
    ({ tagName }) => {
      return !!tagName;
    },
    ({ tagName, key, name, __config }) => {
      return {
        cid: key,
        cname: __config.aliasName || __config.label || name,
        ctype: tagName,
        cmodel: tagName === 'hlLayoutForm' ? __config.modelCode : undefined, // form对应模型code
      }
    }
  );
}

// 组件映射
const COMPONENT_MAP = {
  table: "hlTableList",
  form: "hlLayoutForm",
};

export default {
  DESKTOP: true,
  COMPONENT_MAP,
  getModals,
  getComponents,
  getScopedComponents
};
